//
//  SemPageController.m
//  i5db
//
//  Created by iMuse on 15-5-16.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "SemPageController.h"
#import "SemPageCell.h"

@interface SemPageController ()<SemPageDataSource,UIScrollViewDelegate,SemPageDelegate>
{
    SemPageCell * cellView;
    NSArray     * controllerArray;
    
    UIScrollView * scrollView;
    UIView       * contentView;
}

@end

@implementation SemPageController

-(instancetype)initWithController:(NSArray*)viewController
{
    self = [super init];
    {
        self.title = @"我的聚会";
        controllerArray = viewController;
    }
    
    return self;
}
-(void)updateViewConstraints
{
    [super updateViewConstraints];
    [cellView autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [cellView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [cellView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [cellView autoSetDimension:ALDimensionHeight toSize:30];
    
    [scrollView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:cellView withOffset:0];
    [scrollView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [scrollView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [scrollView autoSetDimension:ALDimensionHeight toSize:SCREEN_HEIGHT-62-30];
    
    [contentView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:scrollView withOffset:0];
    [contentView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:scrollView withOffset:0];
    [contentView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:scrollView withOffset:0];
    [contentView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:scrollView withOffset:0];
    
    [contentView autoSetDimension:ALDimensionHeight  toSize:SCREEN_HEIGHT-62-30];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setNeedsUpdateConstraints];
    [self initView];
 
}
-(void) initView
{
    cellView = [[SemPageCell alloc] initForAutoLayout];
    cellView.dataSource = self;
    cellView.delegate   = self;
    [cellView initView];
    [self.view addSubview:cellView];
    
    scrollView = [[UIScrollView alloc] initForAutoLayout];
    scrollView.delegate = self;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.backgroundColor  = [UIColor greenColor];
    [self.view addSubview:scrollView];
    
    contentView = [UIView newAutoLayoutView];
    contentView.backgroundColor = [UIColor brownColor];
    [scrollView addSubview:contentView];
    
    NSMutableArray * array = [[NSMutableArray alloc] init];
    for (UIViewController* controller in controllerArray )
    {
        
        UIView * view = controller.view;
        
        [contentView addSubview:view];
        [view autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
        [view autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
        
        [array addObject:view];
    }
    
    [array autoDistributeViewsAlongAxis:ALAxisHorizontal withFixedSpacing:0 alignment:NSLayoutFormatAlignAllCenterY];
    
 
     [contentView autoSetDimension:ALDimensionWidth toSize:SCREEN_WIDTH*controllerArray.count];
    
}



-(void)setOffsetIndex:(NSInteger)index
{
    CGFloat offsetX = SCREEN_WIDTH*(index-0.5)+SCREEN_WIDTH*0.5;
    
    [scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    
    NSInteger tag    = (_scrollView.contentOffset.x+SCREEN_WIDTH*0.5)/SCREEN_WIDTH;
   
}

-(UIView*) semPageView:(SemPageCell *)semPageView cellForPageAtIndex:(NSString*)index
{
    
    
    return nil;
}

-(void)semPageView:(SemPageCell *)semPageView didSelectAtIndex:(NSString *)index
{
    [self setOffsetIndex:[index intValue]];
}


-(NSInteger) semPageView:(SemPageCell *)semPageView
{
    return controllerArray.count;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}
- (void)viewWillAppear:(BOOL)animated {
    
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:NO];
    [super viewWillAppear:animated];
    
    
}


- (void)viewWillDisappear:(BOOL)animated {
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    
    [super viewWillDisappear:animated];
}


@end
